//
//  CarWinAuctionVC.m
//  New_Runway
//
//  Created by mack on 2018/2/8.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarWinAuctionVC.h"
#import <CWAuctionSDK/CWAuctionSDK.h>
#import "CarWinCell.h"
#import "UserDetailModel.h"


@interface CarWinAuctionVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) UserDetailModel *model;


@end

@implementation CarWinAuctionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"拍卖";
    self.view.backgroundColor = kColorWhite();
    
    [self.navigationController.navigationBar setBarTintColor:MAIN_NAV_COLOR()];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:22],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_common_back_n"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    [self loadUserInfo];
    if (@available(iOS 11.0, *)) {
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        [[UITableView appearance] setEstimatedRowHeight:0];
        [[UITableView appearance] setEstimatedSectionFooterHeight:0];
        [[UITableView appearance] setEstimatedSectionHeaderHeight:0];
    }

//    [self carWinLogin];
}
-(void)loadUserInfo{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,UserInfoApi];
    [SVProgressHUD show];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:@{@"userId":[NRWCarUser sharedUser].userId} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
       _model = [UserDetailModel mj_objectWithKeyValues:response];
        if (_model.code == 1) {
            
            [self carWinLogin];
            
        }
        
        [SVProgressHUD dismiss];

    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
        [SVProgressHUD dismiss];
    }];
    
    
}

-(void)carWinLogin{
    
    [CWAuctionInterface syncUserMessageWithMobile:_model.result.userMobile realityName:_model.result.userName isCanAuction:_model.result.shop.shopAuctionState operateCityName:_model.result.market.city.cityName detailedAddress:_model.result.shop.shopAddress isProduction:isProduct result:^(BOOL status, NSError *error) {
        if (status) {
            [self initTableview];
        }else{
            NSLog(@"---------登录失败---------");
        }
    }];
    
#pragma mark - 建立socket 连接
    [CWAuctionInterface startServiceConnect];
}
-(void)back{
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = YES;


}


-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableview registerNib:[UINib nibWithNibName:@"CarWinCell" bundle:nil] forCellReuseIdentifier:@"CarWinCell"];
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_offset(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 130;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array = @[@"竞价",@"关注",@"中标",@"出价"];
    CarWinCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarWinCell"];
    cell.iconimage.image = [UIImage imageNamed:array[indexPath.row]];
    
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.navigationController.navigationBar.hidden = NO;

    if (indexPath.row == 0) {
        [self.navigationController pushViewController:[CWAuctionInterface getAuctionSessionControlView] animated:YES];
        
    }else if (indexPath.row == 1){
        [self.navigationController pushViewController:[CWAuctionInterface getAuctionAttentionControlView] animated:YES];

    }else if (indexPath.row == 2){
        [self.navigationController pushViewController:[CWAuctionInterface getAuctionBibSuccessControlView] animated:YES];

    }else if (indexPath.row == 3){
        [self.navigationController pushViewController:[CWAuctionInterface getAuctionBidCarListControlView] animated:YES];

    }
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size {
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
